#include "pertimer.h"

// Set up the timer
PerTimer::PerTimer(QObject *_parent, int tempID)
{
    connect(this, SIGNAL(timeout()), this, SLOT(inc()));
    this->setInterval(20);
    this->setObjectName(QString::number(tempID) + "pt"); // set up object name
    this->setParent(_parent);
    this->start();
    action = 0; // initialize person to falling
}

// Make the person fall
void PerTimer::inc()
{
    switch(action) // Pick which animation action to perform
    {
    case 0: // handle falling
        {
            if (this->object->pos().y() < ((HelicopterForm*)(this->parent()))->height() - (this->object->height()))
            {
                QPoint pos = this->object->pos();
                this->object->setGeometry(pos.x(), pos.y() + ((HelicopterForm*)(this->parent()))->perSpeed, this->object->width(), this->object->height()); // difficulty level from form
            } else
            {
                action = 1;
            }
        }
        break;
    case 1: // handle hitting the ground
        {
            QMovie *tempTest = ((Person*)(this->object))->movie();
            if (tempTest->fileName() != "images/persondie.mng")
            {
                ((Person*)(this->object))->SetPersonMovie("images/persondie.mng"); // update the graphic
                MakeBlood(); // add some blood
            }
            QMovie *tempTestTwo = ((Person*)(this->object))->movie();
            if (tempTestTwo->state() == 0) // check if dead
            {
                action = 2;

                // Decrement number of saved people
                ((HelicopterForm*)(this->parent()))->DecNumSaved();
            }
        }
        break;
    case 2: // handle deleting timer and person (wrap up & memory management)
        {
            ((HelicopterForm*)(this->parent()))->DeletePersonAndTimer(this->object->objectName());
        }
        break;
    default:
        break;
    }

    // do hitbox test for person being caught by the wagon (will set action to 2 which will delete the person)
    Wagon *temp = ((HelicopterForm*)(this->parent()))->findChild<Wagon*>("wagon");
    QPoint wPos = temp->pos();
    QPoint pPos = this->object->pos();
    // wild hitbox test here
    if (pPos.x() >= wPos.x() && pPos.x() <= (wPos.x() + temp->width() - this->object->width()) && pPos.y() >= (wPos.y() + 5 - this->object->height()) && pPos.y() <= wPos.y() - 18)
    {
        // Inc the person saved count
        ((HelicopterForm*)(this->parent()))->IncNumSaved();

        // Memory management
        ((HelicopterForm*)(this->parent()))->DeletePersonAndTimer(this->object->objectName());
    }
}

// Make some blood
void PerTimer::MakeBlood()
{
    Blood *bloody = new Blood(((HelicopterForm*)(this->parent())), this->object->pos().x(), this->object->pos().y());
    bloody->show();
}

int PerTimer::GetAction()
{
    return action;
}

void PerTimer::SetAction(int act)
{
    action = act;
}
